﻿using System.Collections.Generic;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;

namespace Shared.Service.Logging.ApplicationInsights
{
    public class IgnoreRequestsTelemetryProcessor : ITelemetryProcessor
    {
        private static readonly HashSet<string> IgnoredRequests = new HashSet<string>
        {
            "/ordering/api/health",
            "/ordering/metrics",
            "/marketing/api/health",
            "/marketing/metrics",
            "Health/Get"
        };

        private readonly ITelemetryProcessor _next;

        public IgnoreRequestsTelemetryProcessor(ITelemetryProcessor next)
        {
            _next = next;
        }

        public void Process(ITelemetry item)
        {
            if (item is RequestTelemetry request && ShouldBeIgnored(request))
                return;

            _next.Process(item);
        }

        private static bool ShouldBeIgnored(RequestTelemetry request)
        {
            if (!string.IsNullOrEmpty(request?.Url?.ToString()))
            {
                return IgnoredRequests.Contains(request.Url.AbsolutePath);
            }
            return false;
        }
    }
}
